import { defineMock } from "vite-plugin-mock-dev-server";
import { resultSuccess, resultError } from "./utils";

export default defineMock([
	{
		url: "/mock/login",
		method: ["POST"],
		body: ({ body }) => {
			const { name, pwd } = body;
			if (name === "admin" && pwd === "123456") {
				const userInfo = {
					userName: name,
					photo:
						"https://img2.baidu.com/it/u=1978192862,2048448374&fm=253&fmt=auto&app=138&f=JPEG?w=504&h=500",
					time: new Date().getTime(),
					roles: ["admin"],
					authBtnList: ["btn.add", "btn.del", "btn.edit", "btn.link"],
				};
				return resultSuccess({ userInfo, token: Math.random().toString(36).substring(0) });
			} else if (name === "common" && pwd === "123456") {
				const userInfo = {
					userName: name,
					photo: "https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
					time: new Date().getTime(),
					roles: ["common"],
					authBtnList: ["btn.add", "btn.link"],
				};
				return resultSuccess({ userInfo, token: Math.random().toString(36).substring(0) });
			} else if (name === "test" && pwd === "123456") {
				const userInfo = {
					userName: name,
					photo: "https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
					time: new Date().getTime(),
					roles: ["test"],
					authBtnList: ["btn.add", "btn.link"],
				};
				return resultSuccess({ userInfo, token: Math.random().toString(36).substring(0) });
			} else if (name === "commonTest" && pwd === "123456") {
				const userInfo = {
					userName: name,
					photo: "https://img2.baidu.com/it/u=2370931438,70387529&fm=253&fmt=auto&app=138&f=JPEG?w=500&h=500",
					time: new Date().getTime(),
					roles: ["common", "test"],
					authBtnList: ["btn.add", "btn.link"],
				};
				return resultSuccess({ userInfo, token: Math.random().toString(36).substring(0) });
			} else {
				return resultError({}, "用户不存在或密码错误，请重新登录");
			}
		},
	},
	{
		url: "/mock/logout",
		method: ["POST"],
		body: () => {
			return resultSuccess("登出成功");
		},
	},
]);
